<?php
	/**
	 * 银行转账
	 */
    class Transfer extends APIBase {
		
		public $tag = "Transfer";
		public $isLogin = true;
		public $isvip = false;
		
		public function before() 
		{		
		    //$this->initCacheRedis(); 	
			$this->initMysql();
			$this->inithttp();
			return true;
		}

		public function logic() 
		{			
			
			/*$row = $this->mysql->find("select * from player where id = 4852");
			var_dump($row);
			exit;*/
			
			if (!isset($this->param['send_money']))
			{
				$this->returnError(403, "请输入转账金额");
			}
			else
			{
				$money_count = (int)$this->param['send_money'];
			
				if ( $money_count <= 0 )
				{
					$this->returnError(404, "转账金额必须大于0");
				}
			}
			
			$player = $this->data_redis->hMget("hu:{$this->uid}", array('user','bank', 'money','bank_password','bank_salt','is_vip'));
			//扣税
			$bank_taxrate  = Game::$bank_taxrate;
			
			
			if ($player['bank'] < $money_count)
			{
				$this->returnError(405, "转账金额大于银行金额");
			}
			
			if (!isset($this->param['bank_pass']))
			{
				$this->returnError(406, "请输入银行密码");
			}
			else
			{
				
				$bank_pass = $this->param['bank_pass'];
				if (md5($bank_pass.$player['bank_salt'])!=$player['bank_password'])
				{
					$this->returnError(407, "银行密码错误");
				}
			}
			
			$re_info =array();//接收转账日记变量
			$se_info =array();//发送金币
			
			if (!isset($this->param['receive_uid']))
			{
				$this->returnError(401, "请输入接受转账人ID");
			}
			else 
			{
				$receive_uid = (int)$this->param['receive_uid'];
				
				//查找缓存中是否有收红包玩家信息，如果没有就查询数据库
				//$isExists = $this->data_redis->exists("hu:{$receive_uid}");
				$this->getDataRedis($receive_uid);
				$isExists = $this->get_redis->exists("hu:{$receive_uid}");
				

			   
				if (!$isExists ) 
				{					
				   	
					$bd =$this->check($player,$money_count);
					
					if($bd){
					  $this->returnError(688, $bd);
					  exit;	
					}
						
					$row = $this->mysql->find("select * from player where id = $receive_uid");
					
					if (count($row) == 0) 
					{
						$this->returnError(402, '接受转账用户不存在');
					}
					
					$receive = $row[0];		

					//转移金币
					$player['bank'] -= $money_count;
					$receive['bank'] += $money_count-ceil($money_count * $bank_taxrate/100);
					
					
			        $re_info['type'] = 4;
			        $re_info['vid'] = 1;
                    $re_info['tid']  = 0;
					$re_info['seatid']  = 0;
			        $re_info['uid']  =  (int)$receive_uid;
					
					$re_info['money'] = $money_count + $receive['bank'];
			        $re_info['change_money']  =  $receive['money'];
			        $re_info['create_time']     = time();
					
					
					
					
					//更新数据库
					$where['id'] = $receive_uid;
					$sign  = $this->mysql->update('player', $receive, $where);
					//$wybs =uniqid();
					//保存到redis里
					//$this->cache_redis->hMset("bank_rec:$wybs",$re_info);		
					$result = $this->mysql->insert("change_money_rec",$re_info); 			
				}
				else 
				{	
				    $bd = $this->check($player,$money_count);
					
					if($bd){
					  $this->returnError(688, $bd);
					  exit;	
					}
					$receive = $this->get_redis->hMGet("hu:{$receive_uid}", array('bank','money'));		
                    
					//转移金币
					$player['bank'] -= $money_count;
					$receive['bank'] += $money_count-ceil($money_count * $bank_taxrate/100);
					
					$re_info['type'] = 4;
			        $re_info['vid'] = 1;
                    $re_info['tid']  = 0;
					$re_info['seatid']  = 0;
			        $re_info['uid']  =  (int)$receive_uid;
					
					$re_info['money'] = $money_count + $receive['bank'];
			        $re_info['change_money']  =  $receive['money'];
			        $re_info['create_time']     = time();
			     
					//更新缓存
					$this->get_redis->hMset("hu:{$receive_uid}", $receive);
					$this->get_redis->close();
					//
					//$wybs =uniqid();
					//$this->cache_redis->hMset("bank_rec:$wybs",$re_info);	
					$result = $this->mysql->insert("money_change_rec",$re_info); 
					
					
				}
			}
			
			//bank_rec			
			//更新缓存
			//			
			$this->data_redis->hMset("hu:{$this->uid}", $player);			
			//玩家日记记录
			$se_info['type'] = 3;
			$se_info['vid'] = 1;
            $se_info['tid']  = 0;
			$se_info['seatid']  = 0;
			$se_info['uid']  = $this->uid;
			$se_info['money']  =  $player['money'];
			$se_info['change_money']     = -$money_count;
			$se_info['create_time']      = time();
			$result = $this->mysql->insert("money_change_rec",$se_info); 
			
			//var_dump($result);
			//exit;
			//select($table, $columns, $where, $other = '') {
			//$row = $this->mysql->select("change_money_rec","*",array('type'=>3));
			//var_dump($row);
		    //exit;
			//通知玩家
			//$title = "收到红包啦";
			//$content = $player['user'] . "派发了包含" . $money_count . "金币的红包给你";
			//$this->sendMessage($friend_uid, $title, $content);
			
			//$title2 = "派送红包成功";
			//$content2 = "成功派发了包含" . $money_count . "金币的红包给" . $friend['user'];
			//$this->sendMessage($this->uid, $title2, $content2);
			
			//$result = $this->data_redis->hMget("$this->uid", array('money'));

			//$result['money'] = (int)$result['money'];

			//$this->returnData('0','success!');
			unset($player['bank_password']);
			unset($player['bank_salt']);
			//通知玩家
			$url = 'http://'.Config::$ip_server_confing['ip'].':'.Config::$ip_server_confing['myport'].'/pushsrv?action=Transfer&receiver='.$receive_uid.'&param={"user":"'.$player['user'].'","re_money":"'.$money_count.'"}';
			
			
			
			if(!$this->http->get($url)){
				$this->returnError(577, "消息发送失败！");
			 }	
			
			$this->returnData($player,0, "转账成功！");
		}
		//获得每天转账次数
		public function getusercount($uid) {
			
			  $day_begin=strtotime(date('Y-m-d',time()));
            //where your_column>".$day_begin."  change_money_rec
			  $result = $this->mysql->query("select * from money_change_rec where uid=$uid and type=3 and create_time>$day_begin");
			  if($result){
			  $data=array();
			  if ($result) {
				  while ($row = mysql_fetch_assoc($result)) {
					 
					  $data[] = $row['change_money'];
					
				  }
			  }
			  }
			  $dat['num'] = count($data);  
			  $dat['bank'] = 0; 
			  for($i=0;$i<$dat['num'];$i++){
				  $dat['bank']+=$data[$i];
			  }
			 
			  return  array($dat['num'],$dat['bank']);
		}
		
		public function check($userinfo,$money_count) {
		    /*public static bank_minbankmoney =  100000;  //玩家拥有多少钱才可以转账
			 public static $bank_transfertimes =   5;  //每天最多转多少次
			 public static $bank_min_transfertimes =   500000;  //每次最低转账金额
			 public static $bank_maxday =   1000000000;  //每户每天最高转账金额
			 public static $bank_taxrate=   1;  //每次转账扣税
			 public static $bank_is_vip =  0;  //是否只有vip转账*/
			 $bank_minbankmoney  = Game::$bank_minbankmoney;
			 $bank_transfertimes = Game::$bank_transfertimes;
			 $bank_min_transfertimes = Game::$bank_min_transfertimes;
			 $bank_maxday = Game::$bank_maxday;
			 
			 $bank_is_vip = Game::$bank_is_vip;
			 
			 $arr = $this->getusercount($this->uid);
			 
			 if ($arr[0] > $bank_transfertimes) {
				 return $str = '每天只可以转账'.$bank_transfertimes.'次';
			 }

			 if($userinfo['money']+$userinfo['bank']-$money_count<$bank_minbankmoney){
				 return $str = '转账后账户金额要高于'.$bank_minbankmoney.'才可以转账！';
			 }
			 
			 if($money_count<$bank_min_transfertimes){
				 return $str = '每次最低转账金额'.$bank_min_transfertimes.'次';
			 }
			 if($arr[1] + $money_count>$bank_maxday){
				 return $str = '每天只可以转账'.$bank_maxday.'';
			 }
			 if($bank_is_vip && $userinfo['is_vip']){
				 return $str = '每天只可以转账'.$bank_transfertimes.'次';
			 }
			 
			 return 0;
			 
		}
		

    	public function after() {
			
			//$this->deinitCacheRedis();
			$this->deinitDataRedis();
			$this->deinitMysql();
			$this->deinithttp();
		}
	
    }
?>